package com.tingyuge.thinking.spring.validation;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @author xiaodao
 * @version 1.0
 * @date 2021/12/12
 * @description
 * @see org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
 * @see org.springframework.validation.beanvalidation.MethodValidationPostProcessor
 */
public class SpringBeanValidationDemo {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext("classpath:/META-INF/bean-validator.xml");

        Validator validator = xmlApplicationContext.getBean(Validator.class);
        System.out.println(validator instanceof LocalValidatorFactoryBean);
        UserProcessor userProcessor = xmlApplicationContext.getBean(UserProcessor.class);
        userProcessor.processUser(new User());
        xmlApplicationContext.close();

    }
    @Component
    @Validated
    static class UserProcessor{
        public void  processUser(@Valid User user){
            System.out.println(user);
        }
    }


    static class User{
        @NotNull
        public String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
